package com.zengdada.authority.config;

import com.zengdada.authority.config.listener.AuthorityEnvironmentPreparedEvent;
import com.zengdada.authority.exception.RoutineException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.resource.ResourceUrlProvider;


/**
 * @Auther: 12614
 * @Contact: 1261438511@qq.com
 * @Date: 2018/11/9 16:04
 * @Description: 异常拦截处理; 404除外
 */
@ControllerAdvice(annotations = {Controller.class, RestController.class})
//@ResponseBody
@Slf4j
public class WebExceptionHandle {
    @Autowired
    ResourceUrlProvider resourceUrlProvider;

    /**
     * 处理静态资源
     * @return
     */
    @ModelAttribute("urls")
    public ResourceUrlProvider urls() {
        return this.resourceUrlProvider;
    }

    /**
     * 400 - Bad Request
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(HttpMessageNotReadableException.class)
    public ModelAndView handleHttpMessageNotReadableException(HttpMessageNotReadableException e) {
        ModelAndView view = new ModelAndView("authority/error/500");
        //        if (e instanceof BusinessException){p
//            return ServiceResponseHandle.failed("BUSINESS_ERROR", e.getMessage());
//        }
        log.debug("参数解析失败", e);
//        e.printStackTrace();
        view.addObject("errorMsg", "参数解析失败:" + (e.getMessage() != null ? e.getMessage().substring(0, e.getMessage().length() > 150 ? 149 : e.getMessage().length()) : ""));
        return view;
    }


//
//
//
//    /**
//     * 405 - Method Not Allowed
//     */
//    @ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
//    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
//    public ServiceResponse handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
//        logger.error("不支持当前请求方法", e);
//        return ServiceResponseHandle.failed("request_method_not_supported");
//    }
//
//    /**
//     * 415 - Unsupported Media Type
//     */
//    @ResponseStatus(HttpStatus.UNSUPPORTED_MEDIA_TYPE)
//    @ExceptionHandler(HttpMediaTypeNotSupportedException.class)
//    public ServiceResponse handleHttpMediaTypeNotSupportedException(Exception e) {
//        logger.error("不支持当前媒体类型", e);
//        return ServiceResponseHandle.failed("content_type_not_supported");
//    }


    /**
     * 500 - Internal Server Error
     */
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(Exception.class)
    public ModelAndView handleException(Exception e) {
        ModelAndView view = new ModelAndView("authority/error/500");
        String message = e.getMessage();
        if (AuthorityEnvironmentPreparedEvent.profiles_active != null && !AuthorityEnvironmentPreparedEvent.profiles_active.contains("dev"))
            message = (message != null ? message.substring(0, message.length() > 150 ? 149 : message.length()) : "");
        if (e instanceof RoutineException) {
            view.addObject("errorMsg", message);
        } else if (e instanceof MissingServletRequestParameterException) {
            view.addObject("errorMsg", "缺少指定参数：" + message);
        } else {
            view.addObject("errorMsg", "未知异常：" + message);
        }
        log.debug("服务运行异常", e);
//        e.printStackTrace();
        return view;
    }

}